package com.icangya.admin;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icangya.Utils.JSONUtils;
import com.icangya.Value.FootBall;
import com.icangya.domain.*;
import com.icangya.service.IMissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/admin/adminMission")
public class MissionController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IMissionService iMissionService;

    //获取所有任务
    @RequestMapping(value = "getMission",method =RequestMethod.GET)
    public JSONObject getMissionAll(){
        List<FbBackMission> list=iMissionService.getFbBackMission();
            if(list.size()>0){
                Timestamp data=new Timestamp(new Date().getTime());
                JSONArray jsonArray=new JSONArray();
                for (FbBackMission backMission:list) {
                    JSONObject object=new JSONObject();
                    object.put("id",backMission.getId());
                    object.put("missionName",backMission.getMissionName());//任务名
                    object.put("missionContent",backMission.getMissionContent());//任务内容
                    object.put("missionId",backMission.getMissionId());
                    object.put("status",0);
                    FbMissionRecord missionRecord= iMissionService.getMissionRecord(data,backMission.getMissionId());
                    if(missionRecord!=null){
                        object.put("status",1);//该任务已发布
                    }
                    jsonArray.add(object);
                }
                return JSONUtils.getRoot(0,"success",jsonArray);
            }

        return JSONUtils.getRoot(1,"faild","");
    }

    //设置赠送获取抽奖次数
    @RequestMapping(value = "setMissionTurntable",method =RequestMethod.GET)
    public JSONObject setMissionTurntable(@RequestParam int turntableId){
        if(turntableId==0 || turntableId==1) {
            FootBall.MISSION_TURNTABLE = turntableId;
            return JSONUtils.getRoot(0,"success",FootBall.MISSION_TURNTABLE);
        }
        return JSONUtils.getRoot(1,"faild",FootBall.MISSION_TURNTABLE);
    }


    //发布每日任务
    @RequestMapping(value = "setMissionRecord",method =RequestMethod.GET)
    public JSONObject setMissionRecord(@RequestParam long missionId){
        if(missionId<0 || missionId>5){
            return JSONUtils.getRoot(1,"faild","参数有误");
        }
        Timestamp data=new Timestamp(new Date().getTime());
        FbMissionRecord missionRecord= iMissionService.getMissionRecord(data,missionId);
        if(missionRecord!=null){
            return JSONUtils.getRoot(1,"faild","该任务已发布");
        }

        FbBackMission backMission=    iMissionService.getMissionByMissionId(missionId);
        if(backMission!=null){
            //String missionName, String missionContent,Timestamp createTime, long missionId
            FbMissionRecord record=new FbMissionRecord(backMission.getMissionName(),backMission.getMissionContent(),data,backMission.getMissionId());

            FbMissionRecord fbMissionRecord= iMissionService.saveFbMissionRecord(record);
            if(fbMissionRecord!=null){
                return JSONUtils.getRoot(0,"success","发布成功");
            }

        }
        return JSONUtils.getRoot(1,"faild",null);
    }


//获取奖励信息
    @RequestMapping(value = "getMissionAward",method =RequestMethod.GET)
    public JSONObject getMissionAward(){
        List<FbMissionAward> list=iMissionService.getFbMissionAward();
        if(list.size()>0){
            JSONArray jsonArray=new JSONArray();
            for (FbMissionAward missionAward: list) {

                JSONObject jsonObject=new JSONObject();
                jsonObject.put("id",missionAward.getId());
                jsonObject.put("awardId",missionAward.getAwardId());
                jsonObject.put("awardContent",missionAward.getAwardContent());
                jsonObject.put("status",missionAward.getStatus());//领取的状态
                jsonArray.add(jsonObject);
            }
            return JSONUtils.getRoot(0,"success",jsonArray);
        }
        return JSONUtils.getRoot(1,"faild","");
    }

    //设置可领取的奖励信息
    @RequestMapping(value = "updateMissionAward",method =RequestMethod.GET)
    public JSONObject updateMissionAward(@RequestParam long awardId){

        if ("".equals(awardId) || awardId <= 0) {
            return JSONUtils.getRoot(1,"faild","参数有误");
        }
        FbMissionAward fbMissionAward=   iMissionService.getMissionAward(awardId);
        if(fbMissionAward!=null){
            if(fbMissionAward.getStatus()==1){
                fbMissionAward.setStatus(0);
            }else{
                fbMissionAward.setStatus(1);
            }
            boolean bool= iMissionService.updateMissionAward(fbMissionAward);
            if(bool){
                return JSONUtils.getRoot(0,"success","修改成功");
            }
        }
        return JSONUtils.getRoot(1,"faild",null);
    }

    //根据状态查询所有订单
    @RequestMapping(value = "getOrderByStatus",method =RequestMethod.GET)
    public JSONObject getOrderByStatus(HttpSession session,@RequestParam int status,@RequestParam int paymentStatus){
        if(status<0 || status>1 || paymentStatus>1 || paymentStatus<0){
            return JSONUtils.getRoot(1,"faild","参数有误");
        }

        List<FbOrder> list= iMissionService.getFbOrderStatusAll(status,paymentStatus);
        if(list!=null){
            return JSONUtils.getRoot(0,"success",list);
        }
        return JSONUtils.getRoot(1,"faild",null);
    }

    //查询所有订单
    @RequestMapping(value = "getFbOrderAll",method =RequestMethod.GET)
    public JSONObject getFbOrderAll(HttpSession session){


        List<FbOrder> list= iMissionService.getFbOrderAll();
        if(list!=null){
            return JSONUtils.getRoot(0,"success",list);
        }
        return JSONUtils.getRoot(1,"faild",null);
    }

    //发货，设置快递号，快递公司
    @RequestMapping(value = "setOrderByExpress",method =RequestMethod.GET)
    public JSONObject setOrderByExpress(@RequestParam String orderId,@RequestParam String expressId,@RequestParam String expressName){

        if(orderId==null || "".equals(orderId) || expressId==null || "".equals(expressId) || expressName==null || "".equals(expressId)){
            return JSONUtils.getRoot(1,"faild","参数有误");
        }
        FbOrder order= iMissionService.getOrderByOutTrantId(orderId);
          if(order==null){
              return JSONUtils.getRoot(1,"faild","该订单不存在");
          }

        order.setExpressId(expressId);
        order.setExpressName(expressName);
        order.setStatus(0);//设置订单已发货
        //修改订单信息
        boolean bool=iMissionService.updateOrder(order);

        if(bool){
            return JSONUtils.getRoot(0,"success",order);
        }
        return JSONUtils.getRoot(1,"faild",null);
    }

}












